package com.ydche.service.impl;

import com.ydche.dao.ICarProfileDao;
import com.ydche.idl_output.*;
import com.ydche.service.ICarInfoService;
import com.ydche.util.MyTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CarInfoServiceImpl implements ICarInfoService {

    @Autowired
    private ICarProfileDao carProfileDao;

    @Override
    public Long addCarprofile(Long userId, Long customerId, String licenseNo, String vinNo, String engineNo, String registerDate, boolean isTran, String transDate, String brandCode, String cityCode) {
        CarTypesDB carType = carProfileDao.getCartypes(brandCode);
        CarProfileDB obj = getCarProfileDB(userId.intValue(), customerId.intValue(), licenseNo, vinNo,
                engineNo, registerDate, isTran, transDate, carType.getId(), cityCode, "", "");
         return carProfileDao.addCarProfile(obj);
    }



    @Override
    public void updateCarLicenseImg(Long id, String licenseImgFace, String licenseImgBack) {

    }

    @Override
    public Long saveCarProfile(Long userId, Long customerId, String licenseNo, String vinNo, String engineNo,
                               String registerDate, boolean isTran, String transDate, String brandCode, String cityCode) {
        byte special=isTran?(byte)1:(byte)0;
        List<CarProfileDB> carProfileDBList = carProfileDao.getCarProfileList(userId, customerId, licenseNo);
        for(CarProfileDB carProfileDB: carProfileDBList){
            if(carProfileDB.getVehicleFrameNo().equals(vinNo) && carProfileDB.getEngineNo().equals(engineNo) &&
                    carProfileDB.getRegisterDate().equals(registerDate) &&  carProfileDB.getSpecialCarFlag()==special &&
                    carProfileDB.getSpecialCarDate().equals(transDate)){
                return (long)carProfileDB.getId();
            }
        }
        return addCarprofile(userId, customerId, licenseNo, vinNo, engineNo, registerDate, isTran, transDate, brandCode, cityCode);
    }

    @Override
    public CarInfoByLicenseResp getCarInfoByLicense(String licenseNo) {
        return null;
    }

    @Override
    public CarInfoByVinResp getCarInfoByVinNo(String vinNo) {
        return null;
    }

    @Override
    public FuzzyCarBrandResp getFuzzyCarBrand(String brandName, String page, String row) {
        return null;
    }

    @Override
    public CarModelResp getCarModel(String licenseNo, String frameNo) {
        return null;
    }


    private CarProfileDB getCarProfileDB(int userId, int ownerId, String liceseNo, String vinNo, String engineNo, String registerDate,
                                         boolean isTran, String transDate, int vehicleModeId, String cityCode, String licenseImgFace, String licenseImgBack){
        CarProfileDB obj = new CarProfileDB();
        obj.setOwnerId(ownerId);
        obj.setNoLicenseFlag((byte)1);
        obj.setLicenseNo(liceseNo);
        obj.setCityPlate(liceseNo.substring(0,2));
        obj.setVehicleFrameNo(vinNo);
        obj.setEngineNo(engineNo);
        obj.setVehicleModelId(vehicleModeId);
        obj.setRegisterDate(registerDate);
        obj.setIsLoanCar((byte)0);
        if(isTran){
            obj.setSpecialCarFlag((byte)1);
        }else{
            obj.setSpecialCarFlag((byte)0);
        }
        obj.setSpecialCarDate(transDate);
        obj.setStatus((byte)0);
        obj.setCityCode(Integer.parseInt(cityCode));
        obj.setCreateTime(MyTime.getCurrentDateTime());
        obj.setCreateUserId(userId);
        obj.setDrivingLicenseImgURL(licenseImgFace);
        obj.setDrivingLicenseImgBackURL(licenseImgBack);
        return obj;
    }
}
